﻿using System;
using System.Collections.Generic;
using System.Text;
using Peekaboo2008.model;
using System.Data;
using System.Data.SqlClient;
using Peekaboo2008.util;

namespace Peekaboo2008.dao
{
    class HangHoaDAO
    {
        public static HangHoaModel getByBarCode(String _barcode)
        {
            HangHoaModel model = null;
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("Barcode", SqlDbType.NVarChar);
            param[0].Value = _barcode;

            DataTable tb = DataUtil.executeStore("sp_HangHoa_GetByBarcode", param);
            try
            {
                model = new HangHoaModel();
                foreach (DataRow dr in tb.Rows)
                {
                    model.ID = dr["ID"].ToString();
                    model.Ten = dr["Ten"].ToString();
                    model.MaNhomHang = dr["Ma_nhom_hang"].ToString();
                    model.Thue = dr["Thue"].ToString();
                    model.ChietKhau = dr["ChietKhau"].ToString();
                    model.GhiChu = dr["Ghi_chu"].ToString();
                    model.GiaNhap = dr["Gia_nhap"].ToString();
                    model.GiaBanSi = dr["Gia_ban_si"].ToString();
                    model.GiaBanLe = dr["Gia_ban_le"].ToString();
                    model.TonDinhMuc = dr["Ton_dinh_muc"].ToString();
                    model.MoTa = dr["Mo_ta"].ToString();
                    model.Hinh = dr["Hinh"].ToString();
                    model.BarCodeNew = dr["Barcode_new"].ToString();
                    model.BarCodeOld = dr["Barcode_old"].ToString();
                    model.BarCodeDate = dr["Barcode_date"].ToString();
                    model.TienThuong = dr["Tien_thuong"].ToString();
                    model.NgayCapNhapCuoi = dr["Ngay_cap_nhap_cuoi"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
            return model;
        }

        public static HangHoaModel getByID(String _Id)
        {
            HangHoaModel model = null;
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("ID", SqlDbType.Int);
            param[0].Value = _Id;

            DataTable tb = DataUtil.executeStore("sp_HangHoa_GetByID", param);
            try
            {
                model = new HangHoaModel();
                foreach (DataRow dr in tb.Rows)
                {
                    model.ID = dr["ID"].ToString();
                    model.MaHang = dr["Ma_hang"].ToString();
                    model.Ten = dr["Ten"].ToString();
                    model.MaNhomHang = dr["Ma_nhom_hang"].ToString();
                    model.Thue = dr["Thue"].ToString();
                    model.ChietKhau = dr["Chiet_khau"].ToString();
                    model.DonVi = dr["Ma_don_vi_qui_doi"].ToString();
                    model.GhiChu = dr["Ghi_chu"].ToString();
                    model.GiaNhap = dr["Gia_nhap"].ToString();
                    model.GiaBanSi = dr["Gia_ban_si"].ToString();
                    model.GiaBanLe = dr["Gia_ban_le"].ToString();
                    model.TonDinhMuc = dr["Ton_dinh_muc"].ToString();
                    model.MoTa = dr["Mo_ta"].ToString();
                    model.Hinh = dr["Hinh"].ToString();
                    model.BarCodeNew = dr["Barcode_new"].ToString();
                    model.BarCodeOld = dr["Barcode_old"].ToString();
                    model.BarCodeDate = dr["Barcode_date"].ToString();
                    model.TienThuong = dr["Tien_thuong"].ToString();
                    model.NgayCapNhapCuoi = dr["Ngay_cap_nhat_cuoi"].ToString();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace.ToString());
            }
            return model;
        }
        public static DataTable getHangHoaTonKhoOfChiNhanhByMaChiNhanh(String _maChiNhanh)
        {
            SqlParameter[] param = new SqlParameter[1];
            param[0] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[0].Value = _maChiNhanh;

            return DataUtil.executeStore("sp_HangHoa_GetHangHoaTonKhoOfChiNhanhByMaChiNhanh", param);
        }

        public static DataTable getHangHoaTonKhoOfChiNhanhByMaChiNhanh_Barcode(String _maChiNhanh,String _barcode)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[0].Value = _maChiNhanh;
            param[1] = new SqlParameter("Barcode", SqlDbType.NVarChar);
            param[1].Value = _barcode;

            return DataUtil.executeStore("sp_HangHoa_GetHangHoaTonKhoOfChiNhanhByMaChiNhanh_Barcode", param);
        }

        public static DataTable getHangHoaTonKhoOfChiNhanhByMaChiNhanh_MaHang(String _maChiNhanh, String _maHang)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("Ma_chi_nhanh", SqlDbType.Int);
            param[0].Value = _maChiNhanh;
            param[1] = new SqlParameter("Ma_hang", SqlDbType.Int);
            param[1].Value = _maHang;

            return DataUtil.executeStore("sp_HangHoa_GetHangHoaTonKhoOfChiNhanhByMaChiNhanh_MaHang", param);
        }

        public static Boolean InsertHangHoa(HangHoaModel hangHoaModel) 
        {
            try
            {
                SqlParameter[] param = new SqlParameter[14];
                param[0] = new SqlParameter("MaHang", SqlDbType.NVarChar);
                param[0].Value = hangHoaModel.MaHang;
                param[1] = new SqlParameter("TenHang", SqlDbType.NVarChar);
                param[1].Value = hangHoaModel.Ten;
                param[2] = new SqlParameter("TenNhomHang", SqlDbType.NVarChar);
                param[2].Value = hangHoaModel.TenNhomHang;
                param[3] = new SqlParameter("Barcode", SqlDbType.NVarChar);
                param[3].Value = hangHoaModel.BarCodeNew;
                param[4] = new SqlParameter("MoTa", SqlDbType.NVarChar);
                param[4].Value = hangHoaModel.MoTa;
                param[5] = new SqlParameter("Hinh", SqlDbType.NVarChar);
                param[5].Value = hangHoaModel.Hinh;
                param[6] = new SqlParameter("GiaNhap", SqlDbType.NVarChar);
                param[6].Value = hangHoaModel.GiaNhap;
                param[7] = new SqlParameter("GiaBanSi", SqlDbType.NVarChar);
                param[7].Value = hangHoaModel.GiaBanSi;
                param[8] = new SqlParameter("GiaBanLe", SqlDbType.NVarChar);
                param[8].Value = hangHoaModel.GiaBanLe;
                param[9] = new SqlParameter("MaDonViQuiDoi", SqlDbType.Int);
                param[9].Value = hangHoaModel.DonVi;
                param[10] = new SqlParameter("DinhMucTon", SqlDbType.NVarChar);
                param[10].Value = hangHoaModel.TonDinhMuc;
                param[11] = new SqlParameter("GhiChu", SqlDbType.NVarChar);
                param[11].Value = hangHoaModel.GhiChu;
                param[12] = new SqlParameter("Thue", SqlDbType.NVarChar);
                param[12].Value = hangHoaModel.Thue;
                param[13] = new SqlParameter("ChietKhau", SqlDbType.NVarChar);
                param[13].Value = hangHoaModel.ChietKhau;
                return DataUtil.executeNonStore("sp_HangHoa_Insert", param);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static Boolean UpdateHangHoa(HangHoaModel hangHoaModel)
        {
            try
            {
                SqlParameter[] param = new SqlParameter[15];
                param[0] = new SqlParameter("MaHang", SqlDbType.NVarChar);
                param[0].Value = hangHoaModel.MaHang;
                param[1] = new SqlParameter("TenHang", SqlDbType.NVarChar);
                param[1].Value = hangHoaModel.Ten;
                param[2] = new SqlParameter("MaNhomHang", SqlDbType.NVarChar);
                param[2].Value = hangHoaModel.MaNhomHang;
                param[3] = new SqlParameter("Barcode", SqlDbType.NVarChar);
                param[3].Value = hangHoaModel.BarCodeNew;
                param[4] = new SqlParameter("MoTa", SqlDbType.NVarChar);
                param[4].Value = hangHoaModel.MoTa;
                param[5] = new SqlParameter("Hinh", SqlDbType.NVarChar);
                param[5].Value = hangHoaModel.Hinh;
                param[6] = new SqlParameter("GiaNhap", SqlDbType.NVarChar);
                param[6].Value = hangHoaModel.GiaNhap;
                param[7] = new SqlParameter("GiaBanSi", SqlDbType.NVarChar);
                param[7].Value = hangHoaModel.GiaBanSi;
                param[8] = new SqlParameter("GiaBanLe", SqlDbType.NVarChar);
                param[8].Value = hangHoaModel.GiaBanLe;
                param[9] = new SqlParameter("MaDonViQuiDoi", SqlDbType.Int);
                param[9].Value = hangHoaModel.DonVi;
                param[10] = new SqlParameter("DinhMucTon", SqlDbType.NVarChar);
                param[10].Value = hangHoaModel.TonDinhMuc;
                param[11] = new SqlParameter("GhiChu", SqlDbType.NVarChar);
                param[11].Value = hangHoaModel.GhiChu;
                param[12] = new SqlParameter("ID", SqlDbType.NVarChar);
                param[12].Value = hangHoaModel.ID;
                param[13] = new SqlParameter("Thue", SqlDbType.NVarChar);
                param[13].Value = hangHoaModel.Thue;
                param[14] = new SqlParameter("ChietKhau", SqlDbType.NVarChar);
                param[14].Value = hangHoaModel.ChietKhau;
                return DataUtil.executeNonStore("sp_HangHoa_Update", param);
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static DataTable GetAllHangHoa(String maHang,String tenHang) 
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("Ma_hang", SqlDbType.NVarChar);
            param[0].Value = maHang;
            param[1] = new SqlParameter("Ten_hang", SqlDbType.NVarChar);
            param[1].Value = tenHang;
            return DataUtil.executeStore("sp_HangHoa_GetAll", param);
        }
    }

}
